package ru.pb.auth.model;

import java.util.List;

import org.apache.log4j.Logger;

import ru.pb.auth.model.enums.S_CHANNEL_TYPE;
import ru.pb.auth.network.clients.GameServerClient;
import ru.pb.global.network.IPRange;

public class GameServerInfo
{
	private static Logger _log = Logger.getLogger(GameServerInfo.class);
	// gameserver info
	private int _id;
	// private int _ageLimin;
	private String _password;
	private String _allow_ip;

	private S_CHANNEL_TYPE _type;

	private boolean _isAuthed;

	// status
	private GameServerClient _gst;
	private boolean _isOnline;

	private List<IPRange> _ipRanges;
	private int _port;

	// config
	private boolean _isGMOnly;
	private int _maxPlayers;

	// clear on disconnect
	private boolean _IsTemp;

	public GameServerInfo(int id, String password, String type, String ip)
	{
		_id = id;
		_password = password;
		_type = S_CHANNEL_TYPE.valueOf(type);
		_allow_ip = ip;
	}

	public void setId(int id)
	{
		_id = id;
	}

	public S_CHANNEL_TYPE getType()
	{
		return _type;
	}

	public int getId()
	{
		return _id;
	}

	public String getPassword()
	{
		return _password;
	}

	public String getAllowIp()
	{
		return _allow_ip;
	}

	public void setAuthed(boolean isAuthed)
	{
		_isAuthed = isAuthed;
	}

	public boolean isAuthed()
	{
		return _isAuthed;
	}

	public void setGameServer(GameServerClient gameserver)
	{
		_gst = gameserver;
	}

	public GameServerClient getGameServer()
	{
		return _gst;
	}

	public void setOnline(boolean online)
	{
		_isOnline = online;
	}

	public boolean isOnline()
	{
		return _isOnline;
	}

	public int getCurrentPlayerCount()
	{
		if(_gst == null)
			return 0;
		return _gst.getPlayerCount();
	}

	public int getPort()
	{
		return _port;
	}

	public void setPort(int port)
	{
		_port = port;
	}

	public void setMaxPlayers(int maxPlayers)
	{
		_maxPlayers = maxPlayers;
	}

	public int getMaxPlayers()
	{
		return _maxPlayers;
	}

	public List<IPRange> getIpRanges()
	{
		return _ipRanges;
	}

	public void setIpRanges(List<IPRange> ipRanges)
	{
		_ipRanges = ipRanges;
	}

	public void setDown()
	{
		_log.info("GameServer [" + _id + "] Отключается...");
		setAuthed(false);
		setGameServer(null);
		setOnline(false);
	}

	public void setGameHosts(List<IPRange> r)
	{
		setIpRanges(r);
		setOnline(true);
	}

	public void info()
	{
		_log.info("Успешно зарегестрирован сервер [serverId = " + _id + "]");

		for(IPRange range : _ipRanges)
			_log.info("GameServer info[" + _id + "] range: IP -> " + range.toString() + ":" + _port);
	}

	public byte[] getIPAddressForPlayer(String playerIp)
	{
		if( !isOnline())
		{
			return new byte[] {127, 0, 0, 1};
		}

		for(IPRange ipr : _ipRanges)
		{
			if(ipr.isInRange(playerIp))
				return ipr.getAddress();
		}

		return new byte[] {127, 0, 0, 1};
	}

	public boolean isTemp()
	{
		return _IsTemp;
	}

	public void setIsTemp(boolean isTemp)
	{
		_IsTemp = isTemp;
	}

	public boolean isGMOnly()
	{
		return _isGMOnly;
	}

	public void setGMOnly(boolean GMOnly)
	{
		_isGMOnly = GMOnly;
	}
}